package net.maku.framework.common.entity;

import java.util.HashMap;
import java.util.Map;

public class TwoValueMap<K, V1, V2> {
    private Map<K, Pair<V1, V2>> data;

    public TwoValueMap() {
        this.data = new HashMap<>();
    }

    public void addEntry(K key, V1 value1, V2 value2) {
        data.put(key, new Pair<>(value1, value2));
    }

    public void clear() {
        data.clear();
    }

    public Pair<V1, V2> get(K key) {
        return data.get(key);
    }

    // 新增方法：循环遍历所有条目
    public void forEachEntry(EntryConsumer<K, V1, V2> consumer) {
        for (Map.Entry<K, Pair<V1, V2>> entry : data.entrySet()) {
            K key = entry.getKey();
            Pair<V1, V2> pair = entry.getValue();
            V1 value1 = pair.getV1();
            V2 value2 = pair.getV2();

            // 调用 consumer 处理每个条目
            consumer.accept(key, value1, value2);
        }
    }

    public static void main(String[] args) {
        TwoValueMap<String, String, String> twoValuesMap = new TwoValueMap<>();

        twoValuesMap.addEntry("a", "value1_a", "value2_a");
        twoValuesMap.addEntry("b", "value1_b", "value2_b");
        twoValuesMap.addEntry("b", "value1_b1", "value2_b1");

        // 循环遍历所有条目并打印
        twoValuesMap.forEachEntry((key, value1, value2) -> {
            System.out.println("Key: " + key);
            System.out.println("Value1: " + value1);
            System.out.println("Value2: " + value2);
            System.out.println("---");
        });
        twoValuesMap.clear();
    }

    private static class Pair<V1, V2> {
        private V1 value1;
        private V2 value2;

        public Pair(V1 value1, V2 value2) {
            this.value1 = value1;
            this.value2 = value2;
        }

        public V1 getV1() {
            return value1;
        }

        public V2 getV2() {
            return value2;
        }
    }

    public class Entry {
        private V1 value1;
        private V2 value2;

        public Entry(V1 value1, V2 value2) {
            this.value1 = value1;
            this.value2 = value2;
        }

        public V1 getV1() {
            return value1;
        }

        public V2 getV2() {
            return value2;
        }
    }

    // 内部函数式接口，用于接受 Entry 的消费者
    public interface EntryConsumer<K, V1, V2> {
        void accept(K key, V1 value1, V2 value2);
    }
}
